#include <stdlib.h>
#include <string.h>

#include "linked-stack.h"

int InitStack(LnkStack *stack){
    stack->top = NULL;
    stack->length = 0;
    return(0);
}
int DestroyStack(LnkStack *stack){
    if (stack == NULL || stack->top == NULL || stack->length == 0){
        return (-1);
    }
    free(stack->top);
    stack->top = NULL;
    stack->length = 0;
    return(0);
}
int ClearStack(LnkStack *stack){
    return(DestroyStack(stack));
}
int StackEmpty(LnkStack stack){
    if (stack.top == NULL && stack.length == 0){
        return(1);
    } else{
        return(0);
    }
}
int GetTop(LnkStack stack,ElemType *e){
    if (stack.top == NULL || stack.length == 0){
        return(-1);
    }
    memcpy(e,&stack.top->node_value, sizeof(ElemType));
    return(0);
}
int Push(LnkStack *stack,ElemType e){
    LnkNode *tmp = (LnkNode *)malloc(sizeof(LnkNode));
    if (tmp == NULL) return(-1);
    memcpy(&tmp->node_value,&e, sizeof(ElemType));
    tmp->next_node = stack->top;
    stack->top = tmp;
    ++stack->length;
    return(0);
}
int Pop(LnkStack *stack,ElemType *e){
    if (stack->top == NULL || stack->length == 0 || stack == 0){
        return(-1);
    }
    LnkNode *tmp = stack->top->next_node;
    memcpy(e,&stack->top->node_value, sizeof(ElemType));
    free(stack->top);
    stack->top = tmp;
    --stack->length;
    return(0);
}
int StackLength(LnkStack stack){
    return stack.length;
}
